package data;

public class ConversionTools {
	/**
	 * Allows for the conversion between alphabetic column names
	 * and numeric ones.  Characters are easier to read and reference 
	 * while Excel Java requires numerical input
	 * 
	 * @param s column name in alphabetic
	 * @return
	 */
	public static int alphaToNumeric(String s){
		int col = 0;
		int counter = s.length()-1;
		for(int i=0;i<s.length();i++){
			System.out.println(((int)s.charAt(i))-64);
			col+=(((int)s.charAt(i))-64)*(Math.pow(26, counter));
			counter--;
		}
		return col-1;
	}
	/**
	 * Allows for the conversion between alphabetic column names
	 * and numeric ones.  Characters are easier to read and reference 
	 * while Excel Java requires numerical input
	 * 
	 * @param s column name in alphabetic
	 * @return
	 */
	public static String numericToAlpha(int s){
		return numericToAlphaHelper("",s);
		
//		return numericToAlphaHelper2(s,findMaxPowerBase26(s,0),"");
	}
	private static String numericToAlphaHelper(String ret, int s){
		//calc first char
		int numOfChars = (int)Math.floor(s/26); //length of text string
		if(numOfChars>0){
			String first = "";
			first += (char)(numOfChars-1+65);  //65 is ASCII 'a'
			int secondChar = s%26; //Remainder determines 2nd char
			first += (char)(secondChar+65); //combine 1st/2nd chars
			return first;
		}else{
			String first = "";
			first += (char)(s+65); //Length = 1, trivial case
			return first;
		}
	}
//	private static int findMaxPowerBase26(int number, int power){
//		if(number==0){
//			return 0;
//		}
//		if(Math.pow(26,power)<=number){
//			return findMaxPowerBase26(number, power+1);
//		}else{
//			return power-1;
//		}
//	}
//	private static String numericToAlphaHelper2(int number, int power, String output){
//		if(power==0){
//			if(number>25){
//				number--;
//			}
//			int calcNum = (int) Math.floor(number/ (Math.pow(26, power)));
//			output += (char)(calcNum+65);
//			return output;
//		}else{
//			int calcNum = (int)( number % (Math.pow(26, power)));
//			System.out.println(calcNum);
//			output += (char)(calcNum+65);
//			return numericToAlphaHelper2((int)(number-(Math.pow(26, power))),power-1,output);
//		}
//	}
	/**
	 * Function to test if string is only letters
	 * @param s string to be tested
	 * @return true if string contains only letters, false otherrwise
	 */
	public static boolean isAlpha(String s){
		for(int i=0;i<s.length();i++){
			if(!Character.isLetter(s.charAt(i))){
				return false;
			}
		}
		return true;
	}
}
